package com.mmy.service.impl.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.dao.sys.TSysAddressRepository;
import com.mmy.dao.sys.VSysAddressRepository;
import com.mmy.domain.sys.TSysAddress;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.sys.VSysAddress;
import com.mmy.service.sys.SysAddressService;

@Service
public class SysAddressServiceImpl implements SysAddressService {
	@Autowired
	TSysAddressRepository tSysAddressRepository;
	@Autowired
	VSysAddressRepository vSysAddressRepository;

	/**
	 * 查询该用户的所有地址
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 */
	public List<VSysAddress> findByUserID(int iUserID) {
		// TODO Auto-generated method stub
		return vSysAddressRepository.findByUserID(iUserID);
	}

	/**
	 * 创建地址
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void createAddress(TSysAddress tSysAddtress, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		// 获得最大收货编号
		Object iAddressID = tSysAddressRepository.getMaxAddressID();
		if (iAddressID == null) {
			tSysAddtress.setAddressID(1);
		} else {
			tSysAddtress.setAddressID(Integer.parseInt(iAddressID.toString()) + 1);
		}

		tSysAddtress.setUserID(sessionSysUser.getUserID());
		tSysAddtress.setCreateDate(new Date());

		tSysAddtress.setCreator(sessionSysUser.getUserName());
		tSysAddressRepository.save(tSysAddtress.getAddressID());
		tSysAddressRepository.save(tSysAddtress);
	}

	/**
	 * 通过地址ID查询地址
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public TSysAddress findByAddressID(int iAddressID) {
		// TODO Auto-generated method stub
		return tSysAddressRepository.findByAddressID(iAddressID);
	}

	/**
	 * 提交修改的地址信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateSysAddress(TSysUser sessionSysUser, TSysAddress tSysAddress) {
		// TODO Auto-generated method stub
		int iAddressID = tSysAddress.getAddressID();
		/* 定义一个新的实体类对象 */
		TSysAddress dbSysAddress = tSysAddressRepository.findByAddressID(iAddressID);
		dbSysAddress.setName(tSysAddress.getName());
		dbSysAddress.setMobile(tSysAddress.getMobile());
		dbSysAddress.setZoneID(tSysAddress.getZoneID());
		dbSysAddress.setAddress(tSysAddress.getAddress());
		dbSysAddress.setType(tSysAddress.getType());
		dbSysAddress.setPostcode(tSysAddress.getPostcode());
		dbSysAddress.setMemo(tSysAddress.getMemo());
		dbSysAddress.setModifyDate(new Date());
		dbSysAddress.setReviser(sessionSysUser.getUserName());
		tSysAddressRepository.save(dbSysAddress);
	}

	/**
	 * 删除地址信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void deleteChecked(String checkList) {
		// TODO Auto-generated method stub
		String[] striAddressIDs = checkList.split(",");
		List<Integer> iAddressIDs = new ArrayList<>();
		for (String striAddressID : striAddressIDs) {
			iAddressIDs.add(Integer.parseInt(striAddressID));
		}
		tSysAddressRepository.deleteChecked(iAddressIDs);
	}

}
